package com.luoxiang.socialandroid.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.entity.LocalMedia;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;

import java.util.ArrayList;
import java.util.List;

public class Selectorphoto2Adapter extends RecyclerView.Adapter<Selectorphoto2Adapter.ViewHolder> {
    private View view;
    private ArrayList<LocalMedia> list = new ArrayList<>();

    public void setData(List<LocalMedia> list) {
        this.list = (ArrayList<LocalMedia>) list;
    }

    Selectorphoto2Adapter.OnAddClickListener onItemAddClick;

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position, int size); //传递boolean类型数据给activity
    }

    public void setOnAddClickListener(Selectorphoto2Adapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }


    @NonNull
    @Override
    public Selectorphoto2Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_photo, parent, false);
        Selectorphoto2Adapter.ViewHolder holder = new Selectorphoto2Adapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Selectorphoto2Adapter.ViewHolder holder, int position) {
        String cutPath = list.get(position).getCutPath();
        Glide.with(MyAppliaction.getContext())
                .load(cutPath)
                .into(holder.img_photo);
        holder.itemView.setOnClickListener(v -> {
        });
        holder.icon_clearthis.setOnClickListener(v -> {
            list.remove(position);
            onItemAddClick.onItemClicks(position, list.size());
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img_photo;
        Button icon_clearthis;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img_photo = itemView.findViewById(R.id.img_photo);
            icon_clearthis = itemView.findViewById(R.id.icon_clearthis);
        }
    }
}
